package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupDao;
import com.itheima.mapper.CheckItemAndCheckGroup;
import com.itheima.mapper.CheckItemDao;
import com.itheima.mapper.SetmealAndCheckGroup;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private CheckItemAndCheckGroup checkItemAndCheckGroup;
    @Autowired
    private SetmealAndCheckGroup setmealAndCheckGroup;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 调用DAO层查询 并封装到List中
        List<CheckGroup> checkGroupList = checkGroupDao.findPage(queryPageBean.getQueryString());
        // 构建分页结果
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(checkGroupList);
        // 返回分页结果
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Transactional
    @Override
    //删除检查组时要判断该检查组是否关联检查项 如果关联了检查项则将检查组和检查项之间的关联关系删除
    public void deleteById(Integer id) {
        if (checkGroupDao.findById(id) == null) {
            throw new RuntimeException("检查组不存在");
        }

        //如果检查组关联了检查项
        if (checkItemAndCheckGroup.findCheckItemIdsByCheckGroupId(id) != null) {
            // 先删除检查组和检查项的关联关系
            checkItemAndCheckGroup.deleteByCheckGroupId(id);
        }
        // 再删除检查组
        checkGroupDao.deleteById(id);
    }

    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        // 添加检查组
        //新增检查组时可以添加对应的检查项
        //先根据checkitemIds查询到对应的检查项 然后封装到新增的检查组中 最后再插入到数据库中
        if (checkitemIds != null && checkitemIds.length > 0) {
            List<CheckItem> checkItems = checkItemDao.findByIds(checkitemIds);
            checkGroup.setCheckItems(checkItems);
            checkGroupDao.addCheckGroup(checkGroup);
            //取到checkgroup的id便于查询
            //将checkGroup传进去查询到id
            Integer id = checkGroupDao.findBycheckGroup(checkGroup).get(0);
            //在t_checkgroup_checkitem表中新增检查项和检查组的关系
            for (Integer checkitemId : checkitemIds) {
                checkItemAndCheckGroup.addCheckItemAndCheckGroup(id, checkitemId);
            }
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        // 参数验证
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("Invalid ID: " + id);
        }
        return checkGroupDao.findById(id);
    }

@Override
@Transactional
public void editCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
    try {
        if (checkGroup == null || checkitemIds == null || checkitemIds.length == 0) {
            throw new IllegalArgumentException("无效的检查组或检查项列表为空");
        }

        checkGroupDao.editCheckGroup(checkGroup);
        checkItemAndCheckGroup.deleteByCheckGroupId(checkGroup.getId());
        Integer checkGroupId = checkGroup.getId();
        for (Integer checkitemId : checkitemIds) {
            checkItemAndCheckGroup.addCheckItemAndCheckGroup(checkGroupId, checkitemId);
        }
    } catch (DataAccessException e) {
        throw new RuntimeException("编辑检查组失败", e);
    }
}

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        if(setmealId == null || setmealId <= 0){
            throw new RuntimeException("套餐ID不能为空");
        }
        return setmealAndCheckGroup.findCheckGroupsBySetmealId(setmealId);
    }
}
